﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using Microsoft.MediaCenter.UI;
using MyMoviesBrowserLibrary;

namespace MyMoviesBrowser
{
    public class TVMenu : ContentPage
    {
        List<string> parentalRatings = new List<string>(new string[] { "", "Not Rated", "G", "PG", "PG-13", "NC-17", "R" });

        private int? chosenOption;
        private MyMoviesBrowserDataContext ctx;

        public TVMenu()
        { }
        public TVMenu(int? menuOption)
        {
            ctx = MyMoviesBrowserBusiness.GetNewMyMoviesBrowserDataContext();
            

            Content = new VirtualList(this, null);

            if (!menuOption.HasValue)
            {
                AddCommand("All Titles", 0);
                AddCommand("Genres", 1);
                AddCommand("Ratings", 2);
                AddCommand("TV Ratings", 3);
                AddCommand("Network", 4);
            }
            else
            {
                chosenOption = menuOption;
                switch (menuOption)
                {
                    case 1:
                        var genres = ctx.Genres.Where(g => g.Series_Genres.Count() > 0).OrderBy(g=>g.GenreDesc).ToList();

                        foreach (Genre g in genres)
                        {
                            AddCommand(string.Format("{0} ({1})", g.GenreDesc, g.Series_Genres.Count()),g);
                        }
                        break;
                    case 2:
                        var ratings = ctx.Series.Where(s => s.Rating.HasValue).Select(s => s.Rating).Distinct().OrderByDescending(r => r).ToList();
                        

                        foreach (decimal rating in ratings)
                        {
                            int count = ctx.Series.Where(s=>s.Rating == rating).Count();                            
                            AddCommand(string.Format("{0} ({1})", rating, count), rating);
                        }
                        break;
                    case 3:
                        var tvRatings = ctx.TVContentRatings.Where(t => t.Series.Count() > 0).OrderBy(t => t.Description).ToList();
                        foreach (TVContentRating t in tvRatings)
                        {
                            AddCommand(string.Format("{0} ({1})", t.Description, t.Series.Count()), t);
                        }
                        break;
                    case 4:

                        var networks = ctx.Networks.Where(n => n.Series.Count() > 0).OrderBy(n => n.Name).ToList();

                        foreach(Network network in networks)
                        {
                            AddCommand(string.Format("{0} ({1})", network.Name, network.Series.Count), network);
                        }
                        break;                    
                    default:
                        break;
                }
            }
        }

        void command_Invoked(object sender, EventArgs e)
        {
            MenuCommand command = sender as MenuCommand;
            
            if (chosenOption.HasValue)
            {
                List<Series> seriesList = new List<Series>();
                switch (chosenOption.Value)
                {
                    case 1:
                        Genre genre = command.Data as Genre;
                        if (genre == null)
                            return;
                        seriesList = genre.Series_Genres.Select(sg => sg.Series).OrderBy(s => s.OrderName).ToList();
                        break;
                    case 2:
                        decimal ratingValue = (decimal)command.Data;
                        seriesList = ctx.Series.Where(s => s.Rating == ratingValue).OrderBy(s => s.OrderName).ToList();
                        break;
                    case 3:
                        TVContentRating tvRating = command.Data as TVContentRating;
                        if (tvRating == null)
                            return;
                        seriesList = tvRating.Series.OrderBy(s => s.OrderName).ToList();
                        break;
                    case 4:
                        Network network = command.Data as Network;
                        seriesList = network.Series.OrderBy(s => s.OrderName).ToList();
                        break;
                    default:
                        break;
                }
                Application.Current.GoToGalleryPage(new SeriesGallery(seriesList));
                return;
            }
            else
            {
                int? id = (int?)command.Data;
                if (id == 0)
                {

                    Application.Current.GoToGalleryPage(new SeriesGallery(ctx.Series.OrderBy(s => s.OrderName).ToList()));
                    return;
                }
                Application.Current.GoToTVMenu(new TVMenu((int?)id));
            }
        }

        private void AddCommand(string desc, object data)
        {
            MenuCommand command = new MenuCommand() { Description = desc, Owner = this, Data = data };
            command.Invoked += new EventHandler(command_Invoked);
            Content.Add(command);
        }

        public override int JumpTo(string displayValue)
        {
            displayValue = displayValue.ToLower();
            List<string> commands = Content.Cast<MenuCommand>().Select(c => c.Description).ToList();
            List<string> temp = new List<string>();
            int x = -1;
            do
            {
                for (int i = 0; i < commands.Count; i++)
                {
                    if (commands[i].ToLower().StartsWith(displayValue))
                    {
                        x = i;
                        break;
                    }
                }

                if (displayValue.Length > 0)
                    displayValue = displayValue.Substring(0, displayValue.Length - 1);
            } while (x < 0 && displayValue.Length > 0);

            if (x >= 0)
                return x;
            return -1;
        }
    }
}
